import Vue from 'vue'
import Router from 'vue-router'
import TeachingRouter from './TeachingRouter';
Vue.use(Router)
const VueRouterPush = Router.prototype.push //防止重复点击左边菜单时控制台显示的路由报错
Router.prototype.push = function push(to) { //防止重复点击左边菜单时控制台显示的路由报错
  return VueRouterPush.call(this, to).catch(err => err)
}
let router = new Router({
  routes: [
    {
      path: '/',
      name: "home",
      component: () => import('@/pages/index/views/index.vue')
    },
    {
      path: '/login',
      name: "login",
      component: () => import('@/pages/index/views/login.vue')
    },
    {
      //教学管理系统
      path: '/TeachingManagement',
      name: "TeachingManagement",
      component: () => import('@/pages/index/views/TeachingManagement/index.vue')
    },
    {
      //装备维护训练系统
      path: '/EquipmentTrain',
      name: "EquipmentTrain",
      component: () => import('@/pages/index/views/EquipmentTrain/index.vue')
    },
    {
      //资源管理系统
      path: '/ResourceManag',
      name: "ResourceManag",
      component: () => import('@/pages/index/views/ResourceManag/index.vue')
    },
    {
      //声隐身装备的综合管理
      path: '/SoundEq',
      name: "SoundEq",
      component: () => import('@/pages/index/views/EquipmentTrain/SoundEq.vue')
    },
    {
      //在线理论考核
      path: '/OnlineTheory',
      name: "OnlineTheory",
      component: () => import('@/pages/index/views/EquipmentTrain/OnlineTheory.vue')
    },
    {
      //原理示教
      path: '/PrincipleTeach',
      name: "PrincipleTeach",
      component: () => import('@/pages/index/views/EquipmentTrain/PrincipleTeach.vue')
    },
    {
      //装备维护训练
      path: '/EquipmentMain',
      name: "EquipmentMain",
      component: () => import('@/pages/index/views/EquipmentTrain/EquipmentMain.vue')
    }
    // ...TeachingRouter,
  ]
});
// 全局路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token'); // 获取 token
  // 如果目标路由需要登录权限
  if (token) {
    // 有 token，允许进入
    next();
  } else {
    // 没有 token，跳转到登录页
    if (to.path !== '/login') {
      // 不是去登录页面的情况下跳转到登录页面，防止循环重定向
      next({ name: 'login' });
    } else {
      // 如果已经在登录页面了，继续跳转，以避免无限循环
      next();
    }
  }
});
export default router;
